#include "gamecontrol.h"
#include <QRandomGenerator>

GameControl::GameControl(QObject *parent)
    : QObject{parent}
{}

// 初始化玩家
void GameControl::Player_Init()
{
    //对象实例化
    robot_left_ = new Robot("机器人A",this);
    robot_right_ = new Robot("机器人B",this);
    user_ = new UserPlayer("自己",this);

    //头像显示
    robot_left_->Set_Direction(Player::Left);
    robot_right_->Set_Direction(Player::Right);
    user_->Set_Direction(Player::Right);

    //性别
    Player::Sex sex;
    sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
    robot_left_->Set_Sex(sex);
    sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
    robot_right_->Set_Sex(sex);
    sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
    user_->Set_Sex(sex);

    //出牌顺序
    //user
    user_->Set_Prev_Player(robot_left_);
    user_->Set_Next_Player(robot_right_);

    //left robot
    robot_left_->Set_Prev_Player(robot_right_);
    robot_left_->Set_Next_Player(user_);

    //right robot
    robot_right_->Set_Prev_Player(user_);
    robot_right_->Set_Next_Player(robot_left_);

    //指定当前玩家
    current_player_ = user_;
}

Robot *GameControl::Get_Left_Robot()
{
    return robot_left_;
}

Robot *GameControl::Get_Right_Robot()
{
    return robot_right_;
}

UserPlayer *GameControl::Get_User_Player()
{
    return user_;
}

void GameControl::Set_Current_Player(Player *player)
{
    current_player_ = player;
}

Player *GameControl::Get_Current_Player()
{
    return current_player_;
}

Player *GameControl::Get_Pend_Player()
{
    return pend_player_;
}

const Cards &GameControl::Get_Pend_Cards() const
{
    return pend_cards_;
}

void GameControl::Init_All_Cards()
{
    all_cards_.Clear();


    for(int p = Card::CardPoint::Card_Begin + 1; p < Card::Card_Small_Joker; ++p)
    {
        for(int s = Card::CardSuit::Suit_Begin + 1; s < Card::CardSuit::Suit_End; ++s)
        {
            all_cards_.Add(Card((Card::CardPoint)p,(Card::CardSuit)s));
        }
    }

    all_cards_.Add(Card(Card::Card_Small_Joker,Card::CardSuit::Suit_Begin));
    all_cards_.Add(Card(Card::Card_Big_Joker,Card::CardSuit::Suit_Begin));
}

Card GameControl::Take_One_Card()
{
    return all_cards_.Take_RandCard();
}

Cards GameControl::Get_Surplus_Cards()
{
    return all_cards_;
}

void GameControl::Reset_Cards_Data()
{
    //洗牌
    Init_All_Cards();
    //清空所有玩家手中的牌
    robot_left_->Clear_Cards();
    robot_right_->Clear_Cards();
    user_->Clear_Cards();
    //清空出牌玩家和牌
    pend_player_ = nullptr;
    pend_cards_.Clear();
}

void GameControl::Start_Lord_Card()
{
    current_player_->PrePare_Call_Lord();
}

void GameControl::Become_Lord(Player* player)
{
    player->Set_Role(Player::Lord);
    player->Get_Prev_Player()->Set_Role(Player::Farmer);
    player->Get_Next_Player()->Set_Role(Player::Farmer);

    current_player_ = player;
    player->Store_Dispatch_Cards(Get_Surplus_Cards());

    current_player_->PrePare_Play_Hand();
}

void GameControl::Clear_Player_Score()
{
    robot_left_->Set_Score(0);
    robot_right_->Set_Score(0);
    user_->Set_Score(0);
}
